#Copyright 2009-2010 by Ward, David J ( pymethius@ominian.net )
import logging
log = logging.getLogger(__name__)

from webmud.handlers import base
from twisted.internet.task import LoopingCall

class Sys(base.Base):
    """
       System Handler - Mostly Generic actions galore
    """
        
    def escalate(self, id, response):
        """
            Escalate is exposed to the user upon Authentication and instructs the client for how it should
            build itself.
        """
        log.debug("User is %s" % self.user)        
        self.makeRequest("loader", "sys_root").addCallback(self.loadSubModules)
        

    def loadSubModules(self, response ):        
        """
            Once the client side platform ack's that its loaded, instruct the client to build up it's
            submodules.
        """
        result, message, environ = response
        
        log.debug("Client said the modules were loaded, loading submodules")
        self.makeRequest("loader", "konsole")
    
    def echo(self, id, message):
        """
           A simple tester method to verify the client.makeRequest -> server.sendRespond -> client.handleResponse chain is functioning correctly.
           
           Note:  Good chance this is currently broken due how handleRequest was refactored to split out params to its own argument
        """
        self.sendResponse("Hello", id)
        
    def repeat(self, id, message):
        """
            if message = True then set a timed call back to the client
        """
        if message:
            self.looper = LoopingCall(self._repeat)
            self.looper.start(3)
        else:
            self.looper.stop()
            
    def _repeat(self):
        self.sendRequest("no.body", "HEY!")
        